<?php
namespace  app\zyapi\controller;
/**
 * @Author: user
 * @Date:   2019-01-18 13:24:23
 * @Last Modified by:   user
 * @Last Modified time: 2019-01-18 13:52:06
 */
use think\Controller;
use think\Loader;

/**
 * 语音类
 */
class VoiceController extends Controller
{
	
	const PATH =  'http://oa.300c.cn';
    const APP_ID = '15445027';//API的ID
	const API_KEY = 'tpaQAqxwulvIRchAGDek3kyL';//API的KEY
    const SECRET_KEY = 'D7gqDKR9qmOinnIOjrjtG2tQ9iYqOkmu'; //API的SECRET	

	public function convert(){
		$text = request()->param('text');  //获取要转换的文字
		if(!$text){
			echo json_encode(['status'=>"error",'msg'=>'Not_Text']);
	        exit();
		}
		Loader::import('AipSpeech', __DIR__."/../lib/zy_voice/", '.class.php'); //加载类
        $client = new \AipSpeech(self::APP_ID, self::API_KEY, self::SECRET_KEY);
		$result = $client->synthesis($text, 'zh', 1, array(
            'vol' => 5,
        ));// 识别正确返回语音二进制 错误则返回json 参照下面错误码
		
        $path = 'upload/baidutts/'.time().'audio.mp3'; //路径
		
        if(!is_array($result)){
		
            $file = file_put_contents($path, $result);
			
			if($file) { 
              echo json_encode(['status'=>"success",'message'=>self::PATH.'/'.$path]);
			  exit();
            } else{
			   echo json_encode(['status'=>"error",'msg'=>'not found file']);
			   exit();
		    }
 
        }else{
			echo json_encode(['status'=>"error",'msg'=>'result not in array']);
			exit();
		}
		
		
	}
}
